home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0082_CheckerBoard.pas < prev    next >
Pascal/Delphi Source File  |  1994-02-09  |  6KB  |  262 lines

  1.  
  2. Program CheckerBoard;
  3.  
  4. {=============================================
  5.  
  6.              CheckerBoard Example
  7.            Programmed by David Dahl
  8.                   01/06/94
  9.    This program and source are PUBLIC DOMAIN
  10.  
  11.  ---------------------------------------------
  12.  
  13.    This program is an example of how to make
  14.    a moving 3D checkerboard pattern on the
  15.    screen like many demos do.
  16.  
  17.    This program requires VGA.
  18.  
  19.  =============================================}
  20.  
  21. Uses CRT;
  22.  
  23. Const TileMaxX = 10;  { Horiz Size Of Tile }
  24.       TileMaxY = 10;  { Vert Size Of Tile }
  25.  
  26.       ViewerDist = 400;  { Distance Of Viewer From Screen }
  27.  
  28. Type TileArray = Array [0..TileMaxX-1, 0..TileMaxY-1] of Byte;
  29.  
  30.      PaletteRec  = Record
  31.                          Red,
  32.                          Green,
  33.                          Blue  : Byte;
  34.                    End;
  35.      PaletteType = Array[0..255] of PaletteRec;
  36.  
  37.  
  38. Var Tile    : TileArray;
  39.     TilePal : PaletteType;
  40.  
  41. Procedure GoMode13; Assembler;
  42. ASM
  43.    MOV AX, $0013
  44.    INT $10
  45. End;
  46.  
  47. {-[ Set Value Of All DAC Registers ]--------------------------------------}
  48. Procedure SetPalette (Var PalBuf : PaletteType); Assembler;
  49. Asm
  50.     PUSH DS
  51.  
  52.     XOR AX, AX
  53.     MOV CX, 0300h / 2
  54.     LDS SI, PalBuf
  55.  
  56.     MOV DX, 03C8h
  57.     OUT DX, AL
  58.  
  59.     INC DX
  60.     MOV BX, DX
  61.     CLD
  62.  
  63.     MOV DX, 03DAh
  64.     @VSYNC0:
  65.       IN   AL, DX
  66.       TEST AL, 8
  67.     JZ @VSYNC0
  68.  
  69.     MOV DX, BX
  70.     rep
  71.        OUTSB
  72.  
  73.     MOV BX, DX
  74.     MOV CX, 0300h / 2
  75.  
  76.  
  77.     MOV DX, 03DAh
  78.     @VSYNC1:
  79.       IN   AL, DX
  80.       TEST AL, 8
  81.     JZ @VSYNC1
  82.  
  83.     MOV DX, BX
  84.     REP
  85.        OUTSB
  86.  
  87.     POP DS
  88. End;
  89. {-[ Get Value Of All DAC Registers ]--------------------------------------}
  90. Procedure GetPalette (Var PalBuf : PaletteType); Assembler;
  91. Asm
  92.     PUSH DS
  93.  
  94.     XOR AX, AX
  95.     MOV CX, 0300h
  96.     LES DI, PalBuf
  97.  
  98.     MOV DX, 03C7h
  99.     OUT DX, AL
  100.     INC DX
  101.  
  102.     REP
  103.        INSB
  104.  
  105.     POP DS
  106. End;
  107. {-[ Only Set DAC Regs 1 Through (TileMaxX * TileMaxY) ]-------------------}
  108. Procedure SetTileColors (Var PalBuf : PaletteType); Assembler;
  109. ASM
  110.    PUSH DS
  111.  
  112.    MOV CX, TileMaxX * TileMaxY * 3
  113.    MOV AX, 1
  114.    LDS SI, PalBuf
  115.    INC SI
  116.    INC SI
  117.    INC SI
  118.    MOV DX, 03C8h
  119.    OUT DX, AL
  120.    INC DX
  121.    MOV BX, DX
  122.  
  123.    MOV DX, 03DAh
  124.    @VSYNC0:
  125.      IN   AL, DX
  126.      TEST AL, 8
  127.    JZ @VSYNC0
  128.  
  129.    MOV DX, BX
  130.    REP
  131.       OUTSB
  132.  
  133.    POP DS
  134. End;
  135. {-[ Define The Bitmap Of The Tile ]---------------------------------------}
  136. Procedure DefineTile;
  137. Var CounterX,
  138.     CounterY  : Word;
  139. Begin
  140.      For CounterY := 0 to TileMaxY-1 do
  141.          For CounterX := 0 to TileMaxX-1 do
  142.              Tile[CounterX, CounterY] := 1 + CounterX +
  143.                                          (CounterY * TileMaxX);
  144. End;
  145. {-[ Define The Colors Of The Tile ]---------------------------------------}
  146. Procedure DefinePalette;
  147. Var PalXCounter : Byte;
  148.     PalYCounter : Byte;
  149.     PalSize     : Byte;
  150. Begin
  151.      GetPalette (TilePal);
  152.  
  153.      PalSize := (TileMaxX * TileMaxY);
  154.  
  155.      For PalYCounter := 1 to PalSize do
  156.      With TilePal[PalYCounter] do
  157.      Begin
  158.           Red   := 0;
  159.           Green := 0;
  160.           Blue  := 63;
  161.      End;
  162.  
  163.      For PalYCounter := 0 to ((TileMaxY - 1) DIV 2) do
  164.          For PalXCounter := 0 to ((TileMaxX - 1) DIV 2) do
  165.          Begin
  166.               With TilePal[1 + PalXCounter + (PalYCounter*TileMaxX)] do
  167.               Begin
  168.                    Red   := 63;
  169.                    Green := 63;
  170.                    Blue  := 63;
  171.               End;
  172.  
  173.               With TilePal[1 + (TileMaxX DIV 2) +
  174.                                PalXCounter +
  175.                                ((TileMaxY DIV 2) * TileMaxX) +
  176.                                (PalYCounter*TileMaxX)] do
  177.               Begin
  178.                    Red   := 63;
  179.                    Green := 63;
  180.                    Blue  := 63;
  181.               End;
  182.          End;
  183.  
  184. End;
  185. {-[ Display Tiles On Screen ]---------------------------------------------}
  186. Procedure DisplayCheckerBoard;
  187. Var CounterX,
  188.     CounterY  : Integer;
  189.  
  190.     X,
  191.     Y,
  192.     Z         : LongInt;
  193. Begin
  194.      For CounterY := 110 to 199 do
  195.      Begin
  196.           Z := -1600 + (CounterY * 16) + ViewerDist;
  197.  
  198.           If Z = 0 THEN Z :=1;
  199.  
  200.           For CounterX := 0 to 319 do
  201.           Begin
  202.  
  203.                X := 159 + (longInt(CounterX - 159 ) * ViewerDist) DIV Z;
  204.  
  205.                Y := (LongInt(CounterY + 100) * ViewerDist) DIV Z;
  206.  
  207.                MEM[$A000:CounterX + (CounterY * 320)] :=
  208.                    Tile[X MOD TileMaxX, Y MOD TileMaxY]
  209.           End;
  210.      End;
  211.  
  212. End;
  213. {-[ Rotate The Palette Of The Board To Give Illusion Of Movement Over It ]-}
  214. Procedure MoveForwardOverBoard;
  215. Type  TempPalType = Array[1..TileMaxX] of PaletteRec;
  216. Var   TempPal     : TempPalType;
  217.       CounterX,
  218.       CounterY    : Word;
  219. Begin
  220.      For CounterX := 1 to TileMaxX do
  221.          TempPal[CounterX] := TilePal[CounterX];
  222.  
  223.      For CounterY := 0 to (TileMaxY-1) do
  224.          For CounterX := 0 to (TileMaxX-1) do
  225.              TilePal[1 + CounterX + (CounterY * TileMaxX)] :=
  226.                     TilePal[1 + CounterX + ((CounterY+1) * TileMaxX)];
  227.  
  228.      For CounterX := 1 to TileMaxX do
  229.          TilePal[CounterX + ((TileMaxY-1) * TileMaxX)] :=
  230.                 TempPal[CounterX];
  231. End;
  232. {-[ Flush the Keyboard Buffer ]--------------------------------------------}
  233. Procedure FlushKeyboard;
  234. Var Key : Char;
  235. Begin
  236.      While KeyPressed do
  237.            Key := ReadKey;
  238. End;
  239.  
  240. {=[ Main Program ]=========================================================}
  241. Begin
  242.  
  243.      GoMode13;
  244.      DefineTile;
  245.      DefinePalette;
  246.  
  247.      SetPalette(TilePal);
  248.  
  249.      DisplayCheckerboard;
  250.  
  251.      FlushKeyboard;
  252.  
  253.      Repeat
  254.            MoveForwardOverBoard;
  255.            SetTileColors(TilePal);
  256.      Until KeyPressed;
  257.  
  258.      FlushKeyboard;
  259.  
  260.      TextMode(C80);
  261. End.
  262.